﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DTO;
namespace DAO
{
    public class KhachHangDAO
    {
        #region Phần của Luân
        public static KhachHangDTO LayThongTinKhachHangTuDataRow(DataRow dr)
        {
            KhachHangDTO kh = null;
            try
            {
                kh = new KhachHangDTO();
                kh.MaKhachHang = dr["MaKhachHang"].ToString();
                kh.TenTaiKhoan = dr["TenTaiKhoan"].ToString();
                kh.MatKhau = dr["MatKhau"].ToString();
                kh.CMND = dr["CMND"].ToString();
                kh.HoTen = dr["HoTen"].ToString();
                kh.NgaySinh = DateTime.Parse(dr["NgaySinh"].ToString());
                kh.GioiTinh = dr["GioiTinh"].ToString();
                kh.DiaChi = dr["DiaChi"].ToString();
                kh.Email = dr["Email"].ToString();
                kh.DienThoai = dr["DienThoai"].ToString();
                kh.TaiKhoanNganLuong = dr["TaiKhoanNganLuong"].ToString();
                kh.DiemUyTin = int.Parse(dr["DiemUyTin"].ToString());
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi khi lấy thông tin khách hàng: " + ex.ToString());
            }
            return kh;
        }


        
        public static IList<SqlParameter> TruyenThamSoChoStored(KhachHangDTO item)
        {
            IList<SqlParameter> arrParameter = new List<SqlParameter>();
            arrParameter.Add(new SqlParameter("@MaKhachHang", item.MaKhachHang));
            arrParameter.Add(new SqlParameter("@TenTaiKhoan", item.TenTaiKhoan));
            arrParameter.Add(new SqlParameter("@MatKhau", item.MatKhau));
            arrParameter.Add(new SqlParameter("@CMND", item.CMND));
            arrParameter.Add(new SqlParameter("@HoTen", item.HoTen));
            arrParameter.Add(new SqlParameter("@NgaySinh", item.NgaySinh));
            arrParameter.Add(new SqlParameter("@GioiTinh", item.GioiTinh));
            arrParameter.Add(new SqlParameter("@DiaChi", item.DiaChi));
            arrParameter.Add(new SqlParameter("@Email", item.Email));
            arrParameter.Add(new SqlParameter("@DienThoai", item.DienThoai));
            //arrParameter.Add(new SqlParameter("@TaiKhoanNganLuong", item.TaiKhoanNganLuong));
            arrParameter.Add(new SqlParameter("@DiemUyTin", item.DiemUyTin));
            return arrParameter;
        }

        public static bool KiemTraDangNhap(string tenDangNhap, string matKhau, ref string hoTen, ref string maKhachHang)
        {
            try
            {
                IList<SqlParameter> arrParameter = new List<SqlParameter>();
                arrParameter.Add(new SqlParameter("@TenTaiKhoan", tenDangNhap));
                arrParameter.Add(new SqlParameter("@MatKhau", matKhau));
                DataTable dt = DataProvider.ExecuteStoreProc("KiemTraDangNhap", arrParameter);
                if (dt.Rows.Count > 0)
                {
                    maKhachHang = dt.Rows[0][0].ToString();
                    hoTen = dt.Rows[0][1].ToString();
                    return true;
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi khi kiểm tra đăng nhập " + ex.ToString());
            }
            return false;
        }


        public static DataTable ThongTinKhachHang(string maKhachHang)
        {
            DataTable dt = null;
            try
            {
                IList<SqlParameter> arrParameter = new List<SqlParameter>();
                arrParameter.Add(new SqlParameter("@MaKhachHang", maKhachHang));
                dt = DataProvider.ExecuteStoreProc("ThongTinKhachHang", arrParameter);
                
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi khi tìm thông tin khách hàng " + ex.ToString());
            }
            return dt;
        }

        //thêm khách hàng
        public static bool ThemKhachHang(ref KhachHangDTO kh)
        {
            try
            {
                IList<SqlParameter> arrParameter = TruyenThamSoChoStored(kh);
                DataTable dt = DataProvider.ExecuteStoreProc("ThemKhachHang", arrParameter);
                return true;
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi khi thêm mới khách hàng " + ex.ToString());
            }
            return false;
        }

        #endregion
    }
}
